<?php

/**
 * uniBoard ACP Language Import Page
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_imp_langs{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;

		// Upload?
		if ( $appCore -> _POST( 'upload') == 1)
		{
			// Import imageset
			$this -> doImport();	
		}
		else
		{
			// Draw form
			$this -> drawForm();
		}
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Performs import
	 *
	 */
	
	function doImport(){
		
		// Create file upload
		$data_file = new api_file_upload( $this -> appCore, 'data_file');
		
		// Configure uploaded
		$data_file -> setExtensions( array( '.xml', '.xml.gz'));
		$data_file -> setUploadSize( 12288);
		
		// Catch file
		$data_file -> catchFile();
		
		// Try-catch
		try
		{
			// Api has error?
			if ( $data_file -> error)
			{
				// Trow exception
				throw new exc_small( $data_file -> last_error);
			}
				
			// Detect if we uploaded file is GZIP compressed
			if ( strtolower( substr( $data_file -> getFileName(), strlen( $data_file -> getFileName()) - 7)) == '.xml.gz')
			{
				// Uncompress file
				if ( function_exists( 'gzfile'))
				{
					$data_file = join( '', gzfile( $data_file -> getFilePath()));
				}
				else
				{
					// Trow exception
					throw new exc_small( $this -> appCore -> lang['acp_imageset_import_gzip_not_avaiable']);
				}
			}
			else
			{
				// Simply load XML file contents
				$data_file = file_get_contents( $data_file -> getFilePath());
			}
			
			// We will handle errors
			libxml_use_internal_errors(false);
			
			// Create xml data file
			@$data = simplexml_load_string( $data_file);
			
			// Error?
			if ( $data === false)
			{
				// Trow exception
				throw new exc_small( $this -> appCore -> lang['acp_import_xml_corrupted']);
			}
			
			// Data is valid?
			if ( !isset( $data -> language_data) || !isset( $data -> language_data -> language_name) || !isset( $data -> language_data -> language_iso) || !isset( $data -> language_files))
			{
				throw new exc_small( $this -> appCore -> lang['acp_import_xml_corrupted']);
			}
			
			// Set api
			$language = new api_language( $this -> appCore);
			
			// Fill it with data
			$language -> setLanguageName( base64_decode( $data -> language_data -> language_name));
			$language -> setLanguageIso( base64_decode( $data -> language_data -> language_iso));
			
			// Additional fields
			if ( isset( $data -> language_data -> language_author))
			{
				$language -> setLanguageAuthor( base64_decode( $data -> language_data -> language_author));
			}
			
			if ( isset( $data -> language_data -> language_email))
			{
				$language -> setLanguageEmail( base64_decode( $data -> language_data -> language_email));
			}
			
			if ( isset( $data -> language_data -> language_www))
			{
				$language -> setLanguageWww( base64_decode( $data -> language_data -> language_www));
			}
			
			// Api got incorrect settings?
			if ( $language -> error)
			{	
				// API has incorrect data from XML
				throw new exc_small( $this -> appCore -> lang['acp_import_xml_corrupted']);
			}
			
			// Create api
			$language -> saveData();
			
			// Create new directory for api
			class_files::newDir( CACHE_PATH . 'lang_' . $language -> language_id);
			
			// File content
			$file_content = '';
			
			// Reiterate files
			foreach ( $data -> language_files -> file as $file_data)
			{
				// File has name?
				if ( isset( $file_data -> file_name[0]) && isset( $file_data -> file_type[0]))
				{
					// Secure file name
					$file_name = $this -> appCore -> secureFilename( base64_decode( $file_data -> file_name));
				
					// Get File type
					$file_type = base64_decode($file_data -> file_type);
					
					// Text File?
					if ( $file_type == 'txt' && isset( $file_data -> string))
					{
						file_put_contents( CACHE_PATH . 'lang_' . $language -> language_id . '/' .$file_name . '.txt', base64_decode( $file_data -> string));
					}
					else if ( $file_type == 'php' && isset( $file_data -> strings))
					{
						// Reiterate strings
						foreach ( $file_data -> strings -> string as $string_data)
						{
							// String correct?
							if ( isset( $string_data -> string_id[0]) && isset( $string_data -> string_content[0]))
							{
								// Put string in file's content
								$file_content .= '$this -> lang[' . var_export( base64_decode( $string_data -> string_id), true) . '] = ' . var_export( base64_decode( $string_data -> string_content), true) . ';
	';
							}
						}
						
						// Save file?
						if ( isset( $file_content[0]))
						{
							// Save file
							file_put_contents( CACHE_PATH . 'lang_' . $language -> language_id . '/' .$file_name . PHP_EXT, '<?php
	
	' . class_cache::makeFileStamp( 'LANGUAGE ' . $language -> language_id . ' STRINGS') . $file_content);
						}
					}
				}
				
				// Clear file
				$file_content = '';
			}
			
			// Create Log
			$this -> appCore -> logAdmin( 'log_admin_imported_language', htmlspecialchars( $language -> language_name));
			
			// Stuff added, trow user to imagesets list
			$this -> appCore -> output -> drawRedirect( $this -> appCore -> adminLink( 'act=langs'), $this -> appCore -> lang['acp_language_file_imported']);			
		}
		catch ( exc_small $error)
		{
			// Draw error
			$this -> appCore -> output -> error( $error -> message, $this -> appCore -> lang['acp_import_languages']);
			
			// Repeat form
			$this -> drawForm();
		}
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Draws import form
	 *
	 */
	
	function drawForm(){
		
		// Start output buffer
		ob_start();
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=imp_langs'), '', true);
		echo $this -> appCore -> drawHidden( 'upload', 1);
		
		// Open Block
		echo $this -> appCore -> output -> openFormBlock();
		
		// Draw Field
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['import_language_file'], $this -> appCore -> drawFileUpload( 'data_file'), $this -> appCore -> lang['import_language_file_expl']);
		
		// Draw Submit Button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['import_data']);
		
		// Close Block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Close Form
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['acp_import_languages'], array(
			$this -> appCore -> adminLink( 'act=imp_langs') => $this -> appCore -> lang['acp_import_languages'],
		), ob_get_clean());
				
	}
	
}